<?php
//рус
include_once(_APP_PATH. 'modules/admin/controllers/blogs/blogs_admin_http.php');
class blogs_admin_http_web extends blogs_admin_http
{   
    protected function ControllerInit()
    {

    }    
    
    protected function PrimaryActionInit ($basicActionName)
    {
        $this->_module->GetDecorator()->SetMenuActiveElement('articles/blogs');     
    }    
    
    protected function GetOnlyRightBlog()
    {
		$blog = $this->GetBlogFromQueryId();  
        if (!$blog) 
    	{
    		$this->Libs()->messanger->createMessage('Блог не найден', MESSAGE_ERROR);
	       	MozgRoute::Get('http')->Redirect(array('module' => 'admin', 
		        									 'ctrl' => 'blogs', 
		        									 'action' => 'out_blogs'));     		
    	}	

    	return $blog;
    }    
    
    /**
		экшен по-умолчанию
	*/
    public function PrAction()
    {
        $this->out_blogsPr();
    }    
    
    public function out_blogsPrAction()
    {
    	$this->GetBlogQueryPage();
    	
        $this->actionTitle = 'Блоги';

        $onPage = MozgCfg::GetProp('admin/blogs//perPage');
               
        $paginator = SysLoader::GetModule('controlls', 'http', '_web')->GetCtrl('paginator');
       	$pattern = MozgRoute::Get('http')->Create(array('module' => 'admin', 
				        									 'ctrl' => 'blogs', 
				        									 'action' => 'out_blogs',
       														 'bpage' => '__page__'));                  
        $paginator->SetUrlPattern($pattern);
       	$pagMapper = $paginator->GetMapper();
        $pagMapper->Set(0, $this->_bpage, $onPage);
        
        $blogs = $this->GetBlogsMapper()->GetBlogs($pagMapper);
        $pagMapper->SetTotalCount($this->GetBlogsMapper()->GetTotal());               
        
        $t = $this->GetTemplator();
        $t->blogs = $blogs;
        $t->page = $this->_bpage;
        $t->pages = $paginator->Generate();
        $this->LoadTemplate($t->render('blogs/_blogs.phtml'));   	
    }
    
    public function add_blogPrAction ($blog = null)
    {
        $this->actionTitle = 'Блоги';

        if (!$blog)
        {
        	$blog = $this->GetBlogFromQueryId();
	   		if (!$blog) $blog = $this->GetBlogsMapper()->GetNew();
        }        
        
        $t = $this->GetTemplator();
        $t->blog = $blog;
        $this->LoadTemplate ($t->render('blogs/_addblog.phtml'));
    }      
    
    public function save_blogPrAction()
    {
       	$blog = $this->GetBlogFromQueryId();
   		if (!$blog) $blog = $this->GetBlogsMapper()->GetNew();	

        $blog->SetTitle(trim($this->Request()->getPost('title')));
        $blog->SetAbout(trim($this->Request()->getPost('about')));
        $blog->SetDescription(trim($this->Request()->getPost('description')));    

    	$userName = trim($this->Request()->getPost('username'));

		list($realUser) = $this->_ctrls->users->GetUsersByName($userName, 1, true);
		if ($realUser)
		{
			$blog->SetUserId($realUser->GetUserId());
			$blog->SetUserName($realUser->GetUserName());		        	
		}
		else 
		{
			$blog->SetUserId(null);
			$blog->SetUserName('');			
		}        
        
		if ($this->GetBlogsMapper()->Save($blog)) 
        {	  
            $this->Libs()->messanger->createMessage('Данные сохранены');
	       	MozgRoute::Get('http')->Redirect(array('module' => 'admin', 
		        									 'ctrl' => 'blogs'));             
        } 
        else
        {
        	if ($this->GetBlogsMapper()->IsError())
        	{
        		$this->Libs()->messanger->createMessage($this->GetBlogsMapper()->GetErrorMessage(), MESSAGE_ERROR);
        	}
        	else
        	{
	            $this->Libs()->messanger->createMessage('Данные не были сохранены', MESSAGE_ERROR);     		
        	}
        	
        	$this->add_blogPr($blog);
            return;
        }
    }    
    
    public function del_blogPrAction()
    {
       	$blog = $this->GetOnlyRightBlog();   

   		if ($this->GetBlogsMapper()->Delete($blog->GetId()))
   		{
			$this->Libs()->messanger->createMessage('Блог удалён');   			
   		}
   		else
   		{
			$this->Libs()->messanger->createMessage('Не удалось удалить блог', MESSAGE_ERROR);   			
   		}
   		
       	MozgRoute::Get('http')->Redirect(array('module' => 'admin', 
	        									 'ctrl' => 'blogs'));     		
    }     
    
    public function out_articlesPrAction()
    {
    	$blog = $this->GetOnlyRightBlog();    	    	
		$this->OutArticles($blog);		
    }
    
    public function add_articlePrAction($entry = null)
    {
    	$blog = $this->GetOnlyRightBlog();
    	$this->AddArticle($blog, $entry);  	
    }
    
    public function media_add_articlePrAction()
    {
    	$blog = $this->GetOnlyRightBlog();
    	$this->AddArticle($blog);  	
    }    
    
    public function save_articlePrAction ()
    {
    	$blog = $this->GetOnlyRightBlog();
    	$this->SaveArticle($blog);
    }     
    
    public function del_articlePrAction()
    {
    	$blog = $this->GetOnlyRightBlog();
    	$this->DelArticle($blog); 	
    }
    
    public function save_theme_articlesPrAction()
    {
    	$blog = $this->GetOnlyRightBlog();    	
    	$this->SaveThemeArticles($blog);
    }
    
    public function del_theme_articlePrAction()
    {
    	$blog = $this->GetOnlyRightBlog();
    	$this->DelThemeArticle($blog);   	
    }   

    public function out_commentsPrAction()
    {
    	$blog = $this->GetOnlyRightBlog();
    	$this->OutComments($blog);   	
    }

    public function del_commentPrAction()
    {
    	$blog = $this->GetOnlyRightBlog();
    	$this->DelComment($blog);   	
    }     
}